package com.xnccc.view;

import java.util.List;
import java.util.Map;

import com.xnccc.audioset.R;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;

/**
 * @author LFQ dialog 大全
 */
public class XNCCCDialog {

	public interface XNCCCDialogListItemListener {
		public void OnChoseListener(Dialog dialog, Map<String, String> item);
	}

	public static Dialog showListDialog(Context context, List<Map<String, String>> data, int index,
			final XNCCCDialogListItemListener listener) {

		final Dialog mDialog = new Dialog(context, R.style.MyDialog);
		mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		mDialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		mDialog.setContentView(R.layout.list_dialog_layout);
		mDialog.setCanceledOnTouchOutside(false);
		mDialog.show();

		mDialog.setOnKeyListener(new OnKeyListener() {

			@Override
			public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {

				// BaseActivity.noHandleTime = System.currentTimeMillis();
				return false;
			}
		});

		ListView listView = (ListView) mDialog.findViewById(R.id.dialog_list);
		listView.setAdapter(new SimpleAdapter(context, data, R.layout.dialog_list_item, new String[] { "text" },
				new int[] { R.id.dialog_text }));
		listView.setOnItemClickListener(new OnItemClickListener() {

			@SuppressWarnings("unchecked")
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				if (listener != null) {
					listener.OnChoseListener(mDialog, (Map<String, String>) arg0.getAdapter().getItem(arg2));
				}
				mDialog.dismiss();
			}
		});
		listView.setSelection(index);
		return mDialog;
	}

}
